package com.business.action.system;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.business.bean.system.DepartmentBean;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.BeanDAO;
import net.sf.rose.jdbc.query.BeanSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.util.WebUtils;

/**
 * 部门管理
 */
@Controller
@RequestMapping("/sys/depart")
public class DepartAction {

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/list.do")
	public List<DepartmentBean> list(HttpServletRequest request, Service service, String departID) {
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(DepartmentBean.class);
		query.createSql("parentID", departID);
		return dao.list();
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/save.do")
	public PageBean save(HttpServletRequest request, Service service, DepartmentBean bean) {
		PageBean res = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(DepartmentBean.class);
		String id = bean.getDepartID();
		if (id == null || id.equals("")) {
			String pid = bean.getParentID();
			query.setSQL("select max(departID) as departID from SYS_DEPARTMENT where parentID=?");
			query.addParameter(pid);
			DepartmentBean dic = dao.get();
			// 每一个点占4位，根结点也占4位长
			if (dic == null || dic.getDepartID() == null) {
				id = pid + "0001";
			} else {
				String d1 = dic.getDepartID();
				String s1 = d1.substring(0, d1.length() - 4);
				String s2 = d1.substring(d1.length() - 4);
				s2 = Integer.parseInt(s2) + 1 + "";
				s2 = "000" + s2;
				s2 = s2.substring(s2.length() - 4);
				id = s1 + s2;
			}
			bean.setDepartID(id);
			query.createInsertSql(bean);
		} else {
			query.createUpdateSql(bean);
		}
		if (dao.update() > 0) {
			res.setData(bean);
			res.setMessage("保存成功！");
		} else {
			res.setSuccess(false);
			res.setMessage("保存失败！");
		}
		return res;
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete.do")
	public PageBean delete(HttpServletRequest request, Service service, String departID) {
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.createDeleteSql(DepartmentBean.class, departID);
		if(!dao.execute()){
			page.setSuccess(false);
			page.setMessage("删除失败！");
			return page;
		}
		return page;
	}

/**
	   * @description 查询部门列表
	   * @author  jiangwenfeng
	   * @date 创建时间：2019/10/17 22:22
	  */
	 @ResponseBody
	 @RequestMapping("/getDepartmentListByMap.do")
	 public List<DepartmentBean> getDepartmentListByMap(HttpServletRequest request, Service service) {
		 Map<String, Object> map = WebUtils.getRequestData(request);
		 BeanDAO dao = new BeanDAO(service);
		 BeanSQL query = dao.getQuerySQL();
		 query.setEntityClass(DepartmentBean.class);
		 query.createSql(map);
		 return dao.list();
	 }
}
